*
* Run SP-DA with no dominated choices (state-funded above self-funded)
*
* Inputs:
*	"${data_flip_${year}}"
*	"${data_drop_${year}}"
*	"${data_contract_dictionary_${year}}"
*	"${welfare_analysis_no_err_${year}}"
*
* Output:
*	"${match_no_dominated_lower_${year}}"

* Correct dominated choices
use "${data_flip_${year}}", clear
append using "${data_drop_${year}}"
keep if eligibility == 1
	
replace rank = rank - 0.5
merge n:1 program_id state_funded using "${data_contract_dictionary_${year}}", keepusing(contract_id)
keep if _m == 3
drop _m
	
append using "${welfare_analysis_no_err_${year}}", ///
	keep(id applicant_id rank contract_id state_funded program_id priority_score capacity)
	
sort id applicant_id contract_id rank
bys id applicant_id contract_id: gen counter = _n
keep if counter == 1
drop counter
	
bys program_id state_funded contract_id: egen capacity_max = max(capacity)
drop capacity sample_hss eligibility
rename capacity_max capacity
lab var capacity "Capacity"
	
* Run SP-DA
do "${helpers_dir}/runSPDA.do"
	
	*preserve
	*	collapse (count) num_admitted_lower = id (min) priority_score_spda capacity_spda, by(contract_id_spda state_funded_spda program_id_spda)
	*	rename state_funded_spda state_funded
	*	rename program_id_spda program_id
	*	rename priority_score_spda priority_score_lower
	*	rename capacity_spda capacity_lower
	*	drop contract_id_spda
	*	save "${data_dir}/PriorityScoreCutoffsLower_${year}.dta", replace
	*restore
	*drop priority_score_spda capacity_spda
	
rename contract_id_spda contract_id_spda_cf
lab var contract_id_spda_cf "Contract id (no dominated choice, state-funded above self-funded)"

rename rank_spda rank_spda_cf
lab var rank_spda_cf "Rank on ROL (no dominated choice, state-funded above self-funded)"

rename state_funded_spda state_funded_spda_cf
lab var state_funded_spda_cf "State-funded (no dominated choice, state-funded above self-funded)"

rename program_id program_id_spda_cf
lab var program_id_spda_cf "Program id (no dominated choice, state-funded above self-funded)"

save "${match_no_dominated_lower_${year}}", replace
